<template>
	<view class="login">
		<view class="login-top">
			<text>登录转客盟</text>
			<text>全新的体验，开启世界大门！</text>
		</view>
		<view class="login-input">
			<view class="login-phone input">
				<text class="iconfont icon-phone"></text>
				<input type="number" v-model="phone" placeholder="请输入手机号" />
			</view>
			<view class="login-pass input">
				<text class="iconfont icon-pass"></text>
				<input type="password" v-model="password" placeholder="请输入密码" />
			</view>
			<view class="btns">
				<view class="retrieve-pass">
					<text>忘记密码？</text>
				</view>
				<view class="btn">
					<button type="default" :loading="loading" @click="login()">登录</button>
				</view>
			</view>
		</view>
		<view class="agreement" :class="$isPC ? 'IsPC':''">
			<view class="checkbox" @click="checkbox(isAgreement)">
				<text v-if="isAgreement" class="iconfont icon-check"></text>
				<text class="check" v-else></text>
			</view>
			<text class="read" @click="checkbox(isAgreement)">请仔细阅读并同意</text>
			<text class="color" @click="goPage('服务协议','service_rule')">《服务协议》</text>
			<text>和</text>
			<text class="color" @click="goPage('隐私政策','privacy_rule')">《隐私政策》</text>
		</view>
	</view>
</template>

<script>
	import {
		login
	} from '@/api/entrance.js';
	import md5 from "@/utils/w_md5.js"
	import {
		systemInfo,
	} from "@/utils/util.js"
	// import {
	// 	clearData,
	// } from "@/mining/BrowserMining.js";
	// import {
	// 	appClearData,
	// } from "@/mining/AppMining.js";
	import {
		mapMutations
	} from 'vuex';
	export default {
		name: "login",
		data() {
			return {
				loading: false,
				phone: "",
				password: "",
				isAgreement:false
			}
		},
		onNavigationBarButtonTap(e) {
			uni.navigateTo({
				url: "/pages/entrance/register/register"
			})
		},
		methods: {
			...mapMutations(['setUserInfo_mutation', 'setIsOpenMini_mutation']),
			async login() {
				if (!this.phone) {
					this.$msg("手机号不能为空！")
					return;
				}
				if (!(/^1[3456789]\d{9}$/.test(this.phone))) {
					this.$msg("手机号格式不正确！")
					return;
				}
				if (!this.password) {
					this.$msg("密码不能为空！")
					return;
				}
				if(!this.isAgreement){
					this.$msg("请阅读并同意服务协议")
					return
				}
				this.loading = true;
				let res = await login({
					phone: this.phone,
					password: this.password
				});
				if (res.code === 1) {
					this.loading = false;
					this.setUserInfo_mutation(res.data);
					uni.setStorageSync("oauth_token", res.data.token);
					//生成标识 进行对比上次登录账号是否为同一个
					let iden = md5.hex_md5_32((res.data.id).toString());
					let storIden = uni.getStorageSync("iden");
					//非同账号
					if (storIden !== iden) {
						uni.setStorageSync("iden", iden);
						//清除数据
						// if (systemInfo() === "browser") {
						// 	await clearData()
						// } else {
						// 	await appClearData()
						// }
						this.setIsOpenMini_mutation(false)
					}
					uni.switchTab({
						url: "/pages/users/index"
					})
				} else {
					this.loading = false;
					this.$msg(res.msg)
				}
			},
			checkbox(bool){
				this.isAgreement = !bool;
			},
			goPage(name,keyword){
				uni.navigateTo({
					url:'/pages/public/agreement/agreement?name='+name+'&keyword='+keyword
				})
			}
		}
	}
</script>

<style scoped lang="less" src="./login.less">
</style>
